
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.StringTokenizer;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author David
 */
public class Request {
    private String uri;
    private String command;
    private HashMap<String,String> params;
    private HashMap<String,String> headers;
    //private static final String HEXINDEX = "0123456789abcdefABCDEF";

    public Request(){
        this.uri = "";
        this.command = "";
        this.params = new HashMap<String,String>();
        this.headers = new HashMap<String,String>();
    }

    public String getCommand(){
        return this.command;
    }
    
    public void processRequest(BufferedReader incoming){
        try{
            if(incoming.ready()){
                StringTokenizer parser;
                parser = new StringTokenizer(incoming.readLine());
                this.command = parser.nextToken();
                while(incoming.ready()){
                    parser = new StringTokenizer(incoming.readLine());
                    String header = parser.nextToken();
                    String tag = parser.nextToken();
                    this.headers.put(header, tag);
                    if(this.command.equals("create-job")){
                        String fileContents = "";
                        char [] contents = new char[new Integer(this.headers.get("Content Length"))];
                        String next = incoming.readLine(); //read the next line
                        if(next.isEmpty()){ //if the next line is empty
                            //preapare to read the contents on next line
                            while(incoming.ready()){
                                int bytesRead = incoming.read(contents, 0, (new Integer(this.headers.get("Content Length")))-1);
                            }
                            File jobFile = new File("C:\\server\\jobs\\msroll.xml");
                            Writer writer = new BufferedWriter(new FileWriter(jobFile));
                            writer.write(contents);
                            writer.close();
                            /*
                            while(incoming.ready()){
                                fileContents += incoming.readLine();
                            }
                            System.out.println(fileContents);
                             *
                             */
                        }
                    }
                }
            }else{
                System.out.println("Something happened");
            }
            System.out.println(this.command);
        }catch(IOException ex){
            System.out.println("Could not process request");
        }
    }

    /*
    private String decrypt(String encrypted){
        String iv = "fedcba9876543210";
        IvParameterSpec ivspec;
        KeyGenerator keygen;
        SecretKey key;
        ivspec = new IvParameterSpec(iv.getBytes());
        try {
            keygen = KeyGenerator.getInstance("AES");
            keygen.init(128);
            key = keygen.generateKey();
            SecretKeySpec keyspec = new SecretKeySpec(key.getEncoded(), "AES");
            Cipher cipher;
            byte[] decrypted;
            cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
            decrypted = cipher.doFinal(hexToBytes(encrypted));
            return new String(decrypted);
        }catch (NoSuchAlgorithmException ex){
            Logger.getLogger(Request.class.getName()).log(Level.SEVERE, null, ex);
        }catch (NoSuchPaddingException ex){
            Logger.getLogger(Request.class.getName()).log(Level.SEVERE, null, ex);
        }catch (InvalidKeyException ex){
                Logger.getLogger(Request.class.getName()).log(Level.SEVERE, null, ex);
        }catch (InvalidAlgorithmParameterException ex){
            Logger.getLogger(Request.class.getName()).log(Level.SEVERE, null, ex);
        }catch (IllegalBlockSizeException ex){
            Logger.getLogger(Request.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex){
            Logger.getLogger(Request.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    private static byte[] hexToBytes(String s){
       int l = s.length() / 2;
       byte data[] = new byte[l];
       int j = 0;

       for (int i = 0; i < l; i++) {
           char c = s.charAt(j++);
           int n, b;

           n = HEXINDEX.indexOf(c);
           b = (n & 0xf) << 4;
           c = s.charAt(j++);
           n = HEXINDEX.indexOf(c);
           b += (n & 0xf);
           data[i] = (byte) b;
       }
       return data;
    }
     *
     */
}

